package com.u2b.db.activity;

import java.util.HashMap;
import java.util.List;

import orm.AddProxy;
import orm.QueryProxy;
import orm.SqlCreate;
import orm.UpdateProxy;



public class ActivityDaoImpl
{
    private final AddProxy addProxy;

    public ActivityDaoImpl()
    {
        addProxy = new AddProxy();

    }

    public int createActivity(HashMap<String, Object> value)
    {
        String sql = SqlCreate.createInsertSql("activity_db", value);
        String[] sqlKeys = SqlCreate.createSqlKeys(value);
        // String sql =
        // "insert into activity_db(owner,create_time,photo_URL,audio_URL,video_URL,effective_time,extra_message,participator_type,want_num,latitude,longitude)values(?,?,?,?,?,?,?,?,?,?,?,?)";
        // String[] sqlKeys=new
        // String[]{"owner","create_time","photo_URL","audio_URL","video_URL","effective_time","extra_message","participator_type","want_num","latitude","longitude"};
        int primayKey = addProxy.add(sql, sqlKeys, value);
        return primayKey;
    }

    public int joinThis(HashMap<String, Object> value)
    {
        String sql = "insert into activity_footprint_db(aid,uid,join_type) values(?,?,'0')";
        String[] sqlKeys = new String[]
        { "aid", "uid" };
        int primaryKey = addProxy.add(sql, sqlKeys, value);
        if (primaryKey >= 0)
        {
            String aid = (String) value.get("aid");
            String sql_s = "update activity_db set current_join_num=current_join_num+1 where aid=" + aid;
            new UpdateProxy().update(sql_s);
        }
        return primaryKey + 1;
    }

    public int supportThis(HashMap<String, Object> value)
    {
        String sql = "insert into activity_footprint_db(aid,uid,join_type) values(?,?,'1')";
        String[] sqlKeys = new String[]
        { "aid", "uid" };
        int primaryKey = addProxy.add(sql, sqlKeys, value);
        if (primaryKey >= 0)
        {
            String aid = (String) value.get("aid");
            String sql_s = "update activity_db set current_support_num=current_support_num+1 where aid=" + aid;
            new UpdateProxy().update(sql_s);
        }
        return primaryKey + 1;
    }

    public int commentThis(HashMap<String, Object> value)
    {

        String sql = "insert into activity_comment_message_db(aid,uid,create_time,comment_content,refrence_mid) values(?,?,?,?,?)";
        String[] keys = new String[]
        { "aid", "uid", "create_time", "comment_content", "refrence_mid" };
        int primaryKey = addProxy.add(sql, keys, value);
        if (primaryKey > 0)
        {
            String aid = (String) value.get("aid");
            String sql_s = "update activity_db set current_comment_num=current_comment_num+1 where aid=" + aid;
            new UpdateProxy().update(sql_s);
        }
        return primaryKey;
    }

    public List<HashMap<String, Object>> queryActivityList(HashMap<String, Object> value)
    {

        int page = Integer.valueOf(value.get("page").toString());
        int offset = (page - 1) * 10;
        String sql = "select user_volatile_db.user_name,user_volatile_db.head_URL, activity_db.* from  user_volatile_db,activity_db   where user_volatile_db.uid=activity_db.owner  order by aid desc limit ?,10";
        List<HashMap<String, Object>> result = new QueryProxy().query(sql, offset);
        
        return result;

    }

    public List<HashMap<String, Object>> getDetailedMessage(int aid)
    {
        String sql = "select user_volatile_db.user_name,user_volatile_db.head_URL, activity_comment_message_db.* from user_volatile_db,activity_comment_message_db where aid=? and user_volatile_db.uid in (activity_comment_message_db.uid)";
        List<HashMap<String, Object>> result = new QueryProxy().query(sql, aid);
        return result;
    }

    public List<HashMap<String, Object>> getDetailedJoin(int aid)
    {
        String sql = "select user_volatile_db.uid,user_volatile_db.user_name,user_volatile_db.head_URL from user_volatile_db where user_volatile_db.uid in (select uid from activity_footprint_db where  aid=? and join_type=0)";
        List<HashMap<String, Object>> result = new QueryProxy().query(sql, aid);
        return result;
    }

    public List<HashMap<String, Object>> getDetailSupport(int aid)
    {
        String sql = "select user_volatile_db.uid,user_volatile_db.user_name,user_volatile_db.head_URL from user_volatile_db where  user_volatile_db.uid in (select uid from activity_footprint_db where  aid=?  and join_type=1)";
        List<HashMap<String, Object>> result = new QueryProxy().query(sql, aid);
        return result;
    }
}